package com.woniu.cms.settlement.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.cms.settlement.core.entity.SettlementInformation;
import com.woniu.cms.settlement.core.entity.SettlementWork;
import com.woniu.cms.settlement.core.mapper.SettlementWorkMapper;
import com.woniu.cms.settlement.core.service.impl.SettlementInformationServiceImpl;
import com.woniu.cms.settlement.core.service.impl.SettlementWorkServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 工日 前端控制器
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/settlement-work")
@Api(tags = "工日")
public class SettlementWorkController {
    @Autowired
    private SettlementWorkServiceImpl workService;
    @Autowired
    private SettlementWorkMapper settlementWorkMapper;

    @Autowired
    private SettlementInformationServiceImpl informationService;

    @PostMapping("/order/{id}")
    @ApiOperation("下达成本")
    private Result order(@PathVariable("id") Integer id){
        Integer order = workService.order(id);
        return Result.common(order);
    }
    @ApiOperation("工日新增")
    @PostMapping("/save")
    public Result rene(@RequestBody SettlementWork settlementWork){

        QueryWrapper<SettlementInformation> qw = new QueryWrapper<>();
        qw.eq("project_name", settlementWork.getProjectName());
        SettlementInformation one = informationService.getOne(qw);

        Optional<SettlementInformation> one1 = Optional.ofNullable(one);
        settlementWork.setInforId(one1.get().getId());

        List<SettlementWork> list = workService.list();

        for (SettlementWork work : list) {
            if (work.getProjectName().equals(settlementWork.getProjectName())){
                return Result.error(ResultCode.USER_HAS_EXISTED,settlementWork);
            }
        }
        int save = settlementWorkMapper.insert(settlementWork);
        return Result.common(save);

    }






}

